function [s_jt, share, der_price] = rcnl_der_price_mktwide( ...
    pcoef,      ...
    delta,      ...
    Pi,         ...
    rho,        ...
    xnonlin_jt, ...
    dfull_jt,    ...
    in_samp_mrkwide ...
)

mu      = pagemtimes(xnonlin_jt, reshape(Pi, [], 1, 1)) .* dfull_jt;
dmu   = exp((delta + mu) / (1 - rho)).*in_samp_mrkwide;

I_i1rt  = (1 - rho) * log(sum(dmu));
sgroup  = exp(I_i1rt) ./ (1 + exp(I_i1rt));
scond   = dmu ./ sum(dmu);
share   = scond .* sgroup;
s_jt    = mean(share, 2);

[J, ns, M] = size(share);

ir = 1 / (1 - rho);
rr = rho / (1 - rho);

pshare = (pcoef+Pi(1).*xnonlin_jt(:,1,:)) .* share;
own = sum((ir - share - scond * rr) .* pshare, 2);
eyeJ = eye(J);
eye3D = repmat(eyeJ, 1, 1, M);
cross = - pagemtimes((share + scond .* rr), pagetranspose(pshare));
cross(logical(eye3D)) = own;

der_price = cross / ns;
